package controller

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"map-point/model"
	"map-point/response"
	"map-point/setting"
	"map-point/utils"
	"net/http"
	"os"
	"regexp"

	"github.com/labstack/echo/v4"
)

func getFilePath(fileName string) string {
	return fmt.Sprintf("%s/%s.json", setting.GlobalSetting.DataPath, fileName)
}

var NewGraph = NewController(
	"/graph",
	func(c echo.Context) error {

		type Data struct {
			FileName string `json:"fileName"`
		}

		data := Data{}

		if err := c.Bind(&data); err != nil {
			return c.JSON(401, response.Response{"数据格式化错误", err})
		}

		if data.FileName == "" {
			return c.JSON(401, response.Response{"文件名不能为空", nil})
		}

		filePath := getFilePath(data.FileName)

		has, err := utils.HasFile(filePath)

		if err != nil {
			return c.JSON(500, response.Response{"获取文件信息失败", err})
		}

		if has {
			return c.JSON(500, response.Response{fmt.Sprintf("文件: %s 已存在", data.FileName), nil})
		}

		f, err := os.Create(filePath)
		defer f.Close()

		if err != nil {
			return c.JSON(500, response.Response{"新增失败", err})
		}

		_, err = f.WriteString("[]")

		if err != nil {
			return c.JSON(500, response.Response{"初始写入失败", err})
		}

		return c.JSON(http.StatusOK, response.Response{"OK", data.FileName})
	},
)

var GetGraphNames = NewController(
	"/graphNames",
	func(c echo.Context) error {

		fileInfoList, err := ioutil.ReadDir(setting.GlobalSetting.DataPath)

		if err != nil {
			return c.JSON(500, response.Response{"文件获取失败", err})
		}

		data := make([]string, len(fileInfoList))

		fmt.Println(data, len(data))
		rxg := regexp.MustCompile(`.json`)

		for index, item := range fileInfoList {
			data[index] = rxg.ReplaceAllLiteralString(item.Name(), "")
		}

		return c.JSON(http.StatusOK, response.Response{"ok", data})
	},
)

var GetGraph = NewController(
	"/graph",
	func(c echo.Context) error {

		fileName := c.QueryParam("fileName")

		if fileName == "" {
			return c.JSON(400, response.Response{"文件名不能为空", nil})
		}

		buff, err := ioutil.ReadFile(getFilePath(fileName))

		if err != nil {
			return c.JSON(500, response.Response{"文件获取失败", err})
		}

		data := []model.Point{}

		if err := json.Unmarshal(buff, &data); err != nil {
			return c.JSON(500, response.Response{"文件解析失败", err})
		}

		return c.JSON(200, response.Response{"OK", data})

	},
)

var UpdateGraph = NewController(
	"/graph",
	func(c echo.Context) error {
		data := new(model.UpdateGraph)

		if err := c.Bind(&data); err != nil {
			return c.JSON(500, response.Response{"参数获取失败", err})
		}

		if data.FileName == "" {
			return c.JSON(400, response.Response{"文件名不能为空", nil})
		}

		buff, err := json.Marshal(&data.Points)

		if err != nil {
			return c.JSON(500, response.Response{"文件名不能为空", err})
		}

		if err := ioutil.WriteFile(getFilePath(data.FileName), buff, 0777); err != nil {
			return c.JSON(500, response.Response{"文件写入错误", err})
		}

		fmt.Println(data)

		return c.JSON(200, response.Response{"OK", nil})

	},
)

var DelGraph = NewController(
	"/graph/del",
	func(c echo.Context) error {
		fileName := c.QueryParam("fileName")
		filePath := getFilePath(fileName)

		has, err := utils.HasFile(filePath)

		if err != nil {
			return c.JSON(500, response.Response{"删除失败", err})
		}

		if !has {
			return c.JSON(200, response.Response{"OK", fileName})
		}

		err = os.Remove(filePath)

		if err != nil {
			return c.JSON(500, response.Response{"删除失败", err})
		}

		return c.JSON(200, response.Response{"OK", fileName})
	},
)
